#!/bin/sh

top_builddir="."
top_srcdir="@srcdir@"
top_srcdir=`cd $top_srcdir; pwd`    # make top_srcdir an absolute path
srcdir="@srcdir@"
subdirs=`grep 'SUBDIRS *='  Makefile|sed 's/.*=//'`
topdir=`pwd`
retval_success=77

for dir in $subdirs; do
  echo Testing in $dir
  test_status=1
  if test ! -x "$dir/uselib"; then
    test_status='binary not found'
  fi
  if test "$test_status" = 1; then
    cd $dir
    rm -f uselib.out
    ./uselib > uselib.out
    retval=$?
    if test "$retval" != $retval_success; then
      test_status="failed with exit code $retval"
    else
      reference=$top_srcdir/$dir/uselib.out.expected
      #echo NOTE: the reference file is $reference
      if test -f $reference; then
	cat uselib.out |
         $top_srcdir/filter-test-output |
          diff $reference - > uselib.out.diffs 2>/dev/null
	if test $? != 0; then
	  test_status="output mismatch, see $dir/uselib.out.diffs"
	else
	  rm -f uselib.out.diffs
	fi
      else
        test_status="reference file not found: $reference"
      fi
    fi
  fi
  cd $topdir
  if test "$test_status" = 1; then
    echo ...ok
  else
    echo ...FAIL: $test_status
  fi
done
